package com.xyhuo.leetcode.december;

/**
 * 找出字符串中第一个匹配项的下标
 * https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/
 *
 * @time 2023/12/12
 */
public class StrStr {

    public int strStr(String haystack, String needle) {
        if (needle.length() > haystack.length()) {
            return -1;
        }
        int length = haystack.length() - needle.length();
        int needleLen = needle.length();
        for (int i = 0; i <= length; i++) {
            String tmp = haystack.substring(i, i + needleLen);
            if (tmp.equals(needle)) {
                return i;
            }
        }
        return -1;
    }
}
